#
# Kurella/Rapp: The role of issue salience and competitive advantages in spatial models of political competition
#
# Replication Material: Figures 4 and 5
#
#
######################################


set.seed(889)



library(scales)
library(MASS)



# function of votes depending on salience

###############################
# v: voter ideal points n x k #  
# p: party positionsj x k     #
# b: salience parameter       #
# a: valence                  #
###############################


max.util <- function (v, p, b1, b2, a){
  voters <- v
  parties <- p
  n <- length(voters[,1])
  k <- length(parties[,1])
  
  dist1 <- sqrt( (v[,1]*matrix(1,n,k)-t(matrix(1,k,n)*p[,1]))^2 )
  dist2 <- sqrt( (v[,2]*matrix(1,n,k)-t(matrix(1,k,n)*p[,2]))^2 )
  
  a_mat <- matrix(rep(a,n), nrow=n, byrow=TRUE)
  
  vu <- a_mat - ( b1* dist1^2 + b2*dist2^2 ) 
  
  m.util <- rep(0,n,k)
  for(i in 1:n){
    if(length(which(vu[i,]==max(vu[i,])))>1)   m.util[i] <- sample(which(vu[i,]==max(vu[i,])),1)
    if(length(which(vu[i,]==max(vu[i,])))==1)  m.util[i] <- which(vu[i,]==max(vu[i,]))}
  
  return( list(vote=m.util, d1=dist1, d2=dist2))
} 
###################################################





# Sample electorate

XX <- as.data.frame(cbind(rnorm(5000),rnorm(5000)))
N <- 5000

 
parties <- cbind(c(-1,-0.2,0.2,1,  0.5),
                 c(-1,-0.2,0.2,1,  1.3))
parties_o <- parties

# valence
valence <- c(0,0,0,0,0)
erg <- matrix(0, ncol=7, nrow=100)
B1 <- seq(1,0,length.out=100)
B2 <- seq(0,1,length.out=100)  

for( i in 1:100){
  k <- length(parties[,1])
  beta2 <- B2[i]
  beta1 <- B1[i]
  
  y <- max.util(v=XX, p=parties, b1=beta1, b2=beta2, a=valence)  
  d <- as.data.frame(cbind(y$d1, y$d2, y$vote))
  
  
  erg[i,1:5] <- table(d$V11)
  erg[i,6] <- (beta2/beta1)
  erg[i,7] <- i
}

erg <- as.data.frame(erg)
oline <- erg$V5/N



### Figure 4 ###


par(mfrow=c(1,2))

#j <- 100 
# two-dim
plot(XX[,1], XX[,2], col="gray", 
     xlab="Economic Left-Right", ylab="Core Issue",
     xlim=c(-2,2), ylim=c(-2,2),
     main="a) Standard scenario",
     cex=.7)
points(XX[,1], XX[,2], #col=alpha(votecol, 0.3),
       col=alpha("grey", 0.3),
       cex=.7)
points(parties[,1], parties[,2], pch=19, 
       cex=1.5,col="darkblue")
text(parties[,1], parties[,2], c("A", "B", "C", "D", "X"),
     pos=1)


xlabs <- round((B2), digits=2) 

plot(erg$V7,erg$V5/N, type="l", lwd=2, col="darkblue",
     xlab=expression(beta[core] / beta[econ]),
     ylab="Vote shares", axes=FALSE,
     ylim=c(0.04,0.25),
     main="b) Vote share of X as a function of salience")
axis(1, at=c(1,25,50,75,100), 
     labels=c("0 / 1", "0.25 / 0.75", "0.5 / 0.5",
              "0.75 / 0.25" , "1 / 0"))
axis(2)



# Figure 5 

 
par(mfrow=c(3,2))



#####################################################
#
# Scenario: More extreme position on Niche Issue
#

parties <- cbind(c(-1,-0.2,0.2,1,  0.5),
                 c(-1,-0.2,0.2,1,  1.6))

# valence
valence <- c(0,0,0,0,0)
erg <- matrix(0, ncol=7, nrow=100)
B1 <- seq(1,0,length.out=100)
B2 <- seq(0,1,length.out=100)  

for( i in 1:100){
  k <- length(parties[,1])
  beta2 <- B2[i]
  beta1 <- B1[i]
  
  y <- max.util(v=XX, p=parties, b1=beta1, b2=beta2, a=valence)  
  d <- as.data.frame(cbind(y$d1, y$d2, y$vote))
  
  
  erg[i,1:5] <- table(d$V11)
  erg[i,6] <- (beta2/beta1)
  erg[i,7] <- i
}

erg <- as.data.frame(erg)


 
plot(XX[,1], XX[,2], pch="", 
     xlab="Economic Left-Right", ylab="Core Issue",
     xlim=c(-2,2), ylim=c(-2,2),
     main="a) More vs less extreme position",
     cex=.7)
points(parties_o[,1], parties_o[,2], pch=19, 
       cex=1.5,col="darkgrey")
points(parties[5,1], parties[5,2], pch=19,  cex=1.5,col="darkblue")
points(0.5, 1.1, pch=19,  cex=1.5,col="lightblue")
text(parties_o[,1], parties_o[,2], c("A", "B", "C", "D", "X"),
     pos=c(1,1,1,1,4))
text(c(0.5,0.5), c(1.6,1.1), c("X'", "X''"),
     pos=4)


xlabs <- round((B2), digits=2) 

plot(erg$V7,erg$V5/N, type="l", lwd=2,lty="solid", col="darkblue",
     xlab=expression(beta[core] / beta[econ]),
     ylim=c(0.04,0.25),
     ylab="Vote shares", axes=FALSE,
     main="b) Vote share of X as a function of salience")
axis(1, at=c(1,25,50,75,100), 
     labels=c("0 / 1", "0.25 / 0.75", "0.5 / 0.5",
              "0.75 / 0.25" , "1 / 0"))
axis(2)
lines(erg$V7, oline,  col="grey")


 
parties <- cbind(c(-1,-0.2,0.2,1,  0.5),
                 c(-1,-0.2,0.2,1,  1.1))


for( i in 1:100){
  k <- length(parties[,1])
  beta2 <- B2[i]
  beta1 <- B1[i]
  
  y <- max.util(v=XX, p=parties, b1=beta1, b2=beta2, a=valence)  
  d <- as.data.frame(cbind(y$d1, y$d2, y$vote))
  
  
  erg[i,1:5] <- table(d$V11)
  erg[i,6] <- (beta2/beta1)
  erg[i,7] <- i
}

erg <- as.data.frame(erg)
lines(erg$V7, erg$V5/N,  col="lightblue", lty="dotdash", lwd=2)
legend("topleft", c("X': more extreme", "X'': less extreme"),lwd=2,lty=c("solid", "dotdash"), col=c("darkblue", "lightblue"), bty="n")


# 



#################################################################################################################
#
#   Scenario: More extreme position on Econ Issue
#

parties <- cbind(c(-1,-0.2,0.2,1,  1.1),
                 c(-1,-0.2,0.2,1,  1.3))

# valence
valence <- c(0,0,0,0,0)
erg <- matrix(0, ncol=7, nrow=100)
B1 <- seq(1,0,length.out=100)
B2 <- seq(0,1,length.out=100)  

for( i in 1:100){
  k <- length(parties[,1])
  beta2 <- B2[i]
  beta1 <- B1[i]
  
  y <- max.util(v=XX, p=parties, b1=beta1, b2=beta2, a=valence)  
  d <- as.data.frame(cbind(y$d1, y$d2, y$vote))
  
  
  erg[i,1:5] <- table(d$V11)
  erg[i,6] <- (beta2/beta1)
  erg[i,7] <- i
}

erg <- as.data.frame(erg)


 
plot(XX[,1], XX[,2], pch="", 
     xlab="Economic Left-Right", ylab="Core Issue",
     xlim=c(-2,2), ylim=c(-2,2),
     main="c) More vs less extreme position",
     cex=.7)
points(parties_o[,1], parties_o[,2], pch=19, 
       cex=1.5,col="darkgrey")
points(parties[5,1], parties[5,2], pch=19,  cex=1.5,col="darkblue")
points(0, 1.3, pch=19,  cex=1.5,col="lightblue")
text(parties_o[,1], parties_o[,2], c("A", "B", "C", "D", "X"),
     pos=c(1,1,1,1,3))
text(c(1.1,0), c(1.3,1.3), c("X'", "X''"),
     pos=3)



xlabs <- round((B2), digits=2) 

plot(erg$V7,erg$V5/N, type="l", lwd=2, lty="solid", col="darkblue",
     xlab=expression(beta[core] / beta[econ]),
     ylim=c(0.04,0.25),
     ylab="Vote shares", axes=FALSE,
     main="d) Vote share of X as a function of salience")
axis(1, at=c(1,25,50,75,100), 
     labels=c("0 / 1", "0.25 / 0.75", "0.5 / 0.5",
              "0.75 / 0.25" , "1 / 0"))
axis(2)
lines(erg$V7, oline,  col="grey")

# less extreme position on econ
parties <- cbind(c(-1,-0.2,0.2,1,  0),
                 c(-1,-0.2,0.2,1,  1.3))


for( i in 1:100){
  k <- length(parties[,1])
  beta2 <- B2[i]
  beta1 <- B1[i]
  
  y <- max.util(v=XX, p=parties, b1=beta1, b2=beta2, a=valence)  
  d <- as.data.frame(cbind(y$d1, y$d2, y$vote))
  
  
  erg[i,1:5] <- table(d$V11)
  erg[i,6] <- (beta2/beta1)
  erg[i,7] <- i
}

erg <- as.data.frame(erg)
lines(erg$V7, erg$V5/N,  col="lightblue", lty="dotdash",lwd=2)
legend("topleft", c("X': more extreme", "X'': less extreme"),lwd=2, lty=c("solid", "dotdash"),col=c("darkblue", "lightblue"), bty="n")

# 


# 

#####################################################
#
#  Scenario: Distance to the next competitor on niche issue
#

parties <- cbind(c(-1,-0.2,0.2,1,  0.5),
                 c(-1,-0.2,0.2,0.3,  1.3))


for( i in 1:100){
  k <- length(parties[,1])
  beta2 <- B2[i]
  beta1 <- B1[i]
  
  y <- max.util(v=XX, p=parties, b1=beta1, b2=beta2, a=valence)  
  d <- as.data.frame(cbind(y$d1, y$d2, y$vote))
  
  
  erg[i,1:5] <- table(d$V11)
  erg[i,6] <- (beta2/beta1)
  erg[i,7] <- i
}

erg <- as.data.frame(erg)

 
plot(XX[,1], XX[,2], pch="", 
     xlab="Economic Left-Right", ylab="Core Issue",
     xlim=c(-2,2), ylim=c(-2,2),
     main="e) Distance to next party",
     cex=.7)
points(parties_o[,1], parties_o[,2], pch=19, 
       cex=1.5,col="darkgrey")
points(parties[4,1], parties[4,2], pch=19,  cex=1.5,col="darkblue")
points(1, 1.2, pch=19,  cex=1.5,col="lightblue")
text(parties_o[,1], parties_o[,2], c("A", "B", "C", "D", "X"),
     pos=c(1,1,1,4,1))
text(c(1,1), c(0.3,1.2), c("D'", "D''"),
     pos=4)



xlabs <- round((B2), digits=2) 

plot(erg$V7,erg$V5/N, type="l", lwd=2, lty="solid", col="darkblue",
     xlab=expression(beta[core] / beta[econ]),
     ylim=c(0.04,0.25),
     ylab="Vote shares", axes=FALSE,
     main="f) Vote share of X as a function of salience")
axis(1, at=c(1,25,50,75,100), 
     labels=c("0 / 1", "0.25 / 0.75", "0.5 / 0.5",
              "0.75 / 0.25" , "1 / 0"))
axis(2)
lines(erg$V7, oline,  col="grey")

# less extreme position on econ
parties <- cbind(c(-1,-0.2,0.2,1,  0.5),
                 c(-1,-0.2,0.2,1.2,  1.3))


for( i in 1:100){
  k <- length(parties[,1])
  beta2 <- B2[i]
  beta1 <- B1[i]
  
  y <- max.util(v=XX, p=parties, b1=beta1, b2=beta2, a=valence)  
  d <- as.data.frame(cbind(y$d1, y$d2, y$vote))
  
  
  erg[i,1:5] <- table(d$V11)
  erg[i,6] <- (beta2/beta1)
  erg[i,7] <- i
}

erg <- as.data.frame(erg)
lines(erg$V7, erg$V5/N,  col="lightblue", lty="dotdash", lwd=2)
legend("topleft", c("D': larger distance", "D'': smaller distance"),lwd=2, lty=c("solid", "dotdash"), col=c("darkblue", "lightblue"), bty="n")

